//
//  KRSelectCityVC.m
//  QuickCare
//
//  Created by 张凯锐 on 16/7/11.
//  Copyright © 2016年 lidabiao. All rights reserved.
//

#import "KRSelectCityVC.h"
#import "KRProvince.h"

//天气接口的参数和URL
#define url @"http://api.map.baidu.com/telematics/v3/weather"
#define location @"location"
#define output @"output"
#define json @"json"
#define ak @"ak"
#define key @"17IvzuqmKrK1cGwNL6VQebF9"

@interface KRSelectCityVC () <UITableViewDelegate,UITableViewDataSource>
//省
@property (weak, nonatomic) IBOutlet UITableView *provinceTableView;
//城市
@property (weak, nonatomic) IBOutlet UITableView *cityTableView;
//模型数组
@property (nonatomic,strong) NSArray *province;
//城市模型数组
@property (nonatomic,strong) NSMutableArray *city;
//当前位置
@property (weak, nonatomic) IBOutlet UILabel *currentLabel;
@end

@implementation KRSelectCityVC
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1.0];
    self.provinceTableView.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1.0];
    if (self.currentLocation) {
        self.currentLabel.text = [NSString stringWithFormat:@"当前位置: %@",self.currentLocation];
    } else {
        self.currentLabel.text = [NSString stringWithFormat:@"当前位置: 信号不好..."];
    }
}

- (IBAction)didReturn:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.provinceTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionNone];
}

#pragma mark - 刷新定位
- (IBAction)refreshLocation:(id)sender {
    if ([self.delegate respondsToSelector:@selector(refreshLocationDelegate:)]) {
        [self.delegate refreshLocationDelegate:self];
    }
}

#pragma mark - UITableViewDataSource数据源
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    //如果tableView是省则判断成功,否则就是城市
    return [tableView isEqual:self.provinceTableView] ? self.province.count : self.city.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([tableView isEqual:self.provinceTableView]) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"provinceCell"];
        KRProvince *pro = self.province[indexPath.row];
        UILabel *provinceLabel = (UILabel *)[cell.contentView viewWithTag:100];
        provinceLabel.text = pro.name;
        //设置cell的背景色
        cell.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1.0];
        //cell被选中的背景色
        cell.selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame];
        cell.selectedBackgroundView.backgroundColor = [UIColor whiteColor];
        return cell;
    }
    //城市数据
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cityCell"];
    UILabel *cityLabel = (UILabel *)[cell.contentView viewWithTag:100];
    cityLabel.text = self.city[indexPath.row];
    return cell;
}

#pragma mark - UITableViewDelegate代理
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([tableView isEqual:self.provinceTableView]) {
        //把上一个城市数据清除
        [self.city removeAllObjects];
        //重新添加城市数据
        KRProvince *pro = self.province[indexPath.row];
        [self.city addObjectsFromArray:pro.cities];
        [self.cityTableView reloadData];
    } else {
        //被选中的城市
        NSString *city = self.city[indexPath.row];
        //天气接口参数
        NSDictionary *parameters = @{location:city, output:json, ak:key};
        
        //点击城市请求数据代理
        if ([self.delegate respondsToSelector:@selector(selectCityVCDelegate:getURL:parameter:)]) {
            [self.delegate selectCityVCDelegate:self getURL:url parameter:parameters];
        }
        [self.navigationController popViewControllerAnimated:YES];
    }
}


#pragma mark - 懒加载
//懒加载
- (NSArray *)province {
    if (_province == nil) {
        _province = [KRProvince province];
    }
    return _province;
}
//城市数组懒加载
- (NSMutableArray *)city {
    if (_city == nil) {
        KRProvince *pro = [self.province firstObject];
        _city = [NSMutableArray arrayWithArray:pro.cities];
    }
    return _city;
}

@end
